package com.bi.bitest.bao.widgets;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.android.volley.toolbox.ImageLoader;
import com.bi.bitest.bao.R;
import com.bi.bitest.bao.utils.VolleyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created
 * Author:bi[FR]
 * Date:15-10-23
 */
public class TopView_discover_recommend extends FrameLayout implements ViewPager.OnPageChangeListener {
    private ViewPager vPager;
    private List<ImageView> imgViews;
    private List<String> datas;// 数据源

    private LinearLayout navLayout;
    private LinearLayout.LayoutParams layoutParams;//线性布局中子控件使用的布局参数，作用设置子控件大小，外边距
    private Handler handler = new Handler();

    public TopView_discover_recommend(Context context) {
        super(context);

        // 第二个参数this: 布局资源中根标签内声明的布局参数参考的父控件对象
        // 第三个参数true: 代表是将第一个参数中声明的子控件归属到第二个参数对象中，false不归属
        LayoutInflater.from(context).inflate(R.layout.view_top_discover_recommend, this, true);

        initView();
    }

    private void initView() {
        // 查找相关的UI控件
        vPager = (ViewPager) findViewById(R.id.viewPager);
        navLayout = (LinearLayout) findViewById(R.id.navLayout);
    }

    public void setData(List<String> datas) {
        this.datas = datas;
        createViews();
    }

    private void createViews() {
        // 根据数据源创建ViewPager中显示的UI
        imgViews = new ArrayList<ImageView>();//加入数据源

        layoutParams =new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        //  layoutParams.leftMargin=20;
        layoutParams.rightMargin=10;//px
        layoutParams.width=5;
        layoutParams.height=5;
        ImageView imgView = null;
        ImageView navImg = null;
        for (int i=0;i<datas.size();i++)
        {
            imgView = new ImageView(getContext());
            imgView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imgView.setTag(datas.get(i));

            imgViews.add(imgView);

            navImg = new ImageView(getContext());
            navImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
            if(i==0)
                navImg.setImageResource(R.drawable.page_now);
            else
                navImg.setImageResource(R.drawable.page);

            //设置导航图片的标签： 当前导航图片的位置
            navImg.setTag(i);

            navImg.setLayoutParams(layoutParams);
            navLayout.addView(navImg);
        }

        vPager.setAdapter(new ImageAdapter());
        vPager.addOnPageChangeListener(this);
        loadImgs();
    }

    private void loadImgs() {
        // 加载网络图片加载后放入imageView
        for (String data : datas) {
            //NUtils.get(NUtils.TYPE_IMG,zixun.getPicurl(),this);
            //------------------------------------volley
            for (ImageView imageView : imgViews) {
                if(imageView.getTag().equals(data)){
                    VolleyUtils.getLoader(getContext()).get(data, ImageLoader.getImageListener(
                            imageView, R.mipmap.ic_launcher, android.R.drawable.ic_menu_delete));
                }

            }
            new MyThread().start();
        }
    }

    class ImageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imgViews.get(position));

            return imgViews.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imgViews.get(position));
        }
    }
    //
    class MyThread extends Thread {
        int position = 0;
        @Override
        public void run() {
            try {
                while (TopView_discover_recommend.this != null) {
                    Thread.sleep(5000);
                    handler.post(new Runnable() {// 在主线程中执行的  子线程中调用
                        @Override
                        public void run() {
                            position = (++position) % imgViews.size();
                            if (position == 0)
                                vPager.setCurrentItem(position, false);
                            else
                                vPager.setCurrentItem(position);
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int position) {
        ImageView navImg  = null;
        //遍历 导航布局中所有的子控件，判断子控件的位置是否为选择位置，若是，则改变图片的内容
        for(int i=0;i<navLayout.getChildCount();i++){
            navImg = (ImageView) navLayout.getChildAt(i );//获取布局中指定位置的子控件
            if(i==position)
                navImg.setImageResource(R.drawable.page_now);
            else
                navImg.setImageResource(R.drawable.page);
        }

    }



}
